define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {
    var Controller = {
        index: function () {
            require(['jstree'], function () {
                var subject_id = "";

                //初始化科目tree
                $('#channeltree').jstree({
                    "themes": {
                        "stripes": true
                    },
                    "checkbox": {
                        "keep_selected_style": false,
                    },
                    "types": {
                        "channel": {
                            "icon": "fa fa-th",
                        },
                        "list": {
                            "icon": "fa fa-list",
                        },
                        "link": {
                            "icon": "fa fa-link",
                        },
                        "disabled": {
                            "check_node": false,
                            "uncheck_node": false
                        }
                    },
                    'plugins': ["types"],
                    "core": {
                        "multiple": true,
                        'check_callback': true,
                        "data": Config.channelList
                    }
                });

                //绑定科目tree点击事件
                $('#channeltree').on("changed.jstree", function (e, data) {
                    if(data.node){
                        $(".btn-unit-add").removeClass('btn-disabled')
                        $(".btn-unit-add").removeClass('disabled')
                        subject_id = data.node.id;
                        table.bootstrapTable('refresh', {url: $.fn.bootstrapTable.defaults.extend.index_url+"&subject_id="+subject_id});
                    }
                    return false;
                });

                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: 'yexam/paper_bag/index' + location.search,
                        add_url: 'yexam/paper_bag/add',
                        edit_url: 'yexam/paper_bag/edit',
                        del_url: 'yexam/paper_bag/del',
                        //table: 'question_type',
                    },
                    pageList : [20,40,60,80]
                });

                var table = $("#table");

                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    escape: false,
                    pk: 'id',
                    sortName: 'sort',
                    sortOrder:'asc',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'sort', title: "排序",operate: false, editable: true,width:'60px'},
                            {field: 'id', title: __('Id')},
                            {field: 'paper_name', title: __('试卷包名'),align: 'left',},
                            {field: 'papers', title: __('试卷'),align: 'left',},
                            {field: 'paper_type', title: __('试卷类型'),align: 'left',formatter:Table.api.formatter.search},
                            {field: 'create_time', title: __('创建时间'),operate: false},
                            {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });


                //添加章节按钮
                $(document).on('click', '.btn-unit-add', function () {
                    var url = "yexam/paper_bag/add?subject_id="+subject_id;
                    Fast.api.open(url, __('Add'), {
                        callback:function () {
                            table.bootstrapTable('refresh');
                        }
                    });
                });
                // 为表格绑定事件
                Table.api.bindevent(table);
            });
        },
        add: function () {
            Controller.api.bindevent();
            Controller.api.importExamLibrary();
        },
        edit: function () {
            //======= 更改选中状态
            $(".paper-type input").each(function () {
                let index = Number($(this).val());
                if ($(this).prop('checked'))
                {
                    $(".p"+index).show().siblings().hide();
                    console.log($(this).prop('checked'),index)
                }else {
                    $(".p"+index+" input[type=checkbox]").prop('checked',false);
                }
                if (index == 4)
                {
                    $(".library").val(1)
                }else
                    $(".library").val(0)
            })
            Controller.api.bindevent();
            Controller.api.importExamLibrary()
        },
        api: {
            formatter:{},
            bindevent: function () {
                //========== 试题选择
                $('.papers input[type=checkbox]').change(function () {
                    let ids = [];
                    $('.papers input[type=checkbox]:checked').each(function () {
                        ids.push($(this).val());
                    });
                    $('.papers').val(ids.toString());
                });
                //================ 类型选择
                $('.paper-type input[type=radio]').change(function () {
                    let index = $(this).val();
                    let $cls = $('.p'+index);
                    $cls.show().siblings().hide();
                    $cls.siblings().find('input[type=checkbox]').each(function () {
                        if ($(this).prop('checked'))
                        {
                            $(this).prop('checked',false);
                        }
                    });
                    if (index == 4)
                    {
                        $(".library").val(1)
                    }else
                        $(".library").val(0)
                });

                $("#c-package_ids").data("params", function (obj) {
                    return {custom: {category_id: $("#c-category_id").val()}};
                });
                Form.api.bindevent($("form[role=form]"));
            },
            /////////////////// 通过章节导入试卷  ////////////
            importExamLibrary(){
                $(".import-exam").click(function () {
                    let id = $(this).data('id');
                    let type = $(this).data('type');
                    $.post("yexam/paper_bag/addExamUnit",{id,type},function (response) {
                        Layer.msg(response.msg);
                    });
                });
            }
        }
    };
    return Controller;
});
